#include "../Base/Base.h"
#include "../Base/Logger.h"
#include "PimplTest.h"
#include "SingletonTest.h"
#include "FactoryTest.h"
#include <tbb/task_scheduler_init.h>
#include "TaskTest.h"
#include <zlib/zlib.h>
#include "MySingletonTest.h"

void LokiTest()
{
	/************************* Begin Loki Test ****************************/
	// loki::Pimpl
	CPimplTest at;
	at.Test();

	// loki::Singleton
	CSingletonTest & rST = CSingletonTestS::Instance();
	rST.Test();
	CSingletonTest & rST2 = CSingletonTestS::Instance();
	rST2.Test();
	CSingletonTestS::Instance().Test();

	// loki::Factory
	CProductTest * pPT = CFactoryTestS::Instance().CreateObject(PRODUCT_ID_TEST, 1);
	pPT->Test();
	delete pPT;
	pPT = NULL;

	/************************* End Loki Test ****************************/
}

void TBBTest()
{
	task_scheduler_init ts(3);
	task_list tl;
	task& rTask = *new(task::allocate_root())CTaskTest(1);
	task& rTask2 = *new(task::allocate_root())CTaskTest(2);
	task& rTask3 = *new(task::allocate_root())CTaskTest(3);
	task& rTask4 = *new(task::allocate_root())CTaskTest(4);
	task& rTask5 = *new(task::allocate_root())CTaskTest(5);
	tl.push_back(rTask);
	tl.push_back(rTask2);
	tl.push_back(rTask3);
	tl.push_back(rTask4);
	tl.push_back(rTask5);
	//rTask.set_ref_count(1);
	//task::spawn(tl);
	//task::spawn(rTask2);
	task::spawn_root_and_wait(tl);
	int i=0;
	while (true)
	{
		i++;
		Sleep(10);
		DEFAULT_LOG_DEBUG("Main");
		if (i > 100)
			break;
	}
}

void ZLibTest()
{
	unsigned long a = compressBound(100);
}

void MySingletonTest()
{
	CMySingletonTest & r1 = CMySingletonTest::Instance();
	CMySingletonTest & r2 = CMySingletonTest::Instance();

	ASSERT(&r1 == &r2);

	r1.GetHello(1);

	CMySingletonTest::Instance().GetHello(1);
}


#include "../Base/MemoryLeakDetector.h"
int main()
{
#ifdef WIN32
#ifdef _DEBUG
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif //_DEBUG
#endif //WIN32

	DEFAULT_LOG_DEBUG("Begin Test Main...");

	LokiTest();

	TBBTest();

	ZLibTest();

	MySingletonTest();

	DEFAULT_LOG_DEBUG("End Test Main...");
	
	return 0;
}